package com.example.knowman.ObService.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.knowman.ObService.OBKnowCategoryService;
import com.example.knowman.ObService.OBKnowMainService;
import com.example.knowman.ObService.OBlabelCategoryService;
import com.example.knowman.ReturnMessage.Message;
import com.example.knowman.dao.knowCategoryWithStaffname;
import com.example.knowman.entity.knowCategory;
import com.example.knowman.entity.labelCategory;
import com.example.knowman.mapper.KnowCategoryMapper;
import com.example.knowman.mapper.StaffListMapper;
import com.example.knowman.service.IKnowCategoryService;
import com.example.knowman.service.IStaffListService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class ObKnowCategoryServiceImpl extends ServiceImpl<KnowCategoryMapper, knowCategory> implements OBKnowCategoryService {


    @Override
    public String returnKnowCateNameByKnowCateId(Integer knowCateId) {
        knowCategory temp= new knowCategory();
        temp = this.lambdaQuery().select().eq(knowCategory::getKnowCateId,knowCateId).one();

        return temp.getKnowCateName();
    }

    @Override
    public List<knowCategory> getAll() {
        return this.lambdaQuery().select().list();
    }

    @Override
    public List<knowCategory> getChilds(Integer knowCateId) {
        List<knowCategory> res = new ArrayList<>();
        List<knowCategory> all = this.getAll();
        if (knowCateId!=null){
            for(knowCategory item:all){
                if(item.getKnowCateId() == knowCateId){
                    res.add(item);
                    //for
                    for(knowCategory item2:all){
                        if(item2.getParentId() == knowCateId){
                            res.add(item2);
                            //for
                            for(knowCategory item3:all){
                                if(item3.getParentId() == item2.getKnowCateId()){
                                    res.add(item3);
                                }
                            }
                        }
                    }
                }
            }
        }


        return res;
    }

    @Override
    public Map<Integer, Integer> getGo() {
        Map<Integer, Integer> go = new HashMap<>();
        List<knowCategory> all = this.getAll();
        Integer index=0;
        for(knowCategory item:all){
            if(item.getParentId() == 0){
                go.putIfAbsent(item.getKnowCateId(), index);
                index++;
                //for
                for(knowCategory item2:all){
                    if(item2.getParentId() == item.getKnowCateId()){
                        go.putIfAbsent(item2.getKnowCateId(), index);
                        index++;
                        //for
                        for(knowCategory item3:all){
                            if(item3.getParentId() == item2.getKnowCateId()){
                                go.putIfAbsent(item3.getKnowCateId(), index);
                                index++;
                            }
                        }
                    }
                }
            }
        }

        System.out.println(go);
        return go;
    }

    @Override
    public Map<Integer, String> getIdAndName() {
        return this.lambdaQuery().select().list().stream().collect(Collectors.toMap(knowCategory::getKnowCateId, knowCategory::getKnowCateName));
    }
}
